package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;

import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service
public class SettingManager {

    @Reference
    private QuestionService questionService;

    @Reference
    private NotificationService notificationService;

    @Reference
    private BlackListService  blackListService;

    //查询通用设置
    public SettingVo findSetting() {
        //1. 获取用户的id和Phone
        User user = UserHolder.get();

        //2. 根据用户id查询陌生人问题
        Question question = questionService.findByUserId(user.getId());

        //3. 根据用户id查询设置
        Notification notification = notificationService.findByUserId(user.getId());

        //4. 组装返回结果
        SettingVo settingVo = new SettingVo();
        BeanUtil.copyProperties(user, settingVo);//id  phone
        if (question != null) {
            BeanUtil.copyProperties(question, settingVo);//strangerQuestion
        }
        if (notification != null) {
            BeanUtil.copyProperties(notification, settingVo);//notification三个
        }
        return settingVo;
    }

    //设置陌生人问题
    public void saveOrUpdateQuestion(String content) {
        //1.获取当前用户id
        User user = UserHolder.get();

        //2.根据用户id查询其问题
        Question question = questionService.findByUserId(user.getId());


        if(question == null){
            //3.1查不到==>新增.
             question = new Question();
             question.setUserId(user.getId());
             question.setStrangerQuestion(content);

            questionService.save(question);
        }else{
            //3.2查到了==>更新
            question.setStrangerQuestion(content);
            questionService.update(question);
        }



    }

    //设置通知
    public void saveOrUpdateNotification(Notification notificationParam) {
        //1.获取用户id
        User user = UserHolder.get();

        //2.根据用户id通知的设置情况(0/1)
        Notification notification = notificationService.findByUserId(user.getId());


        if(notification == null){
            //3.1不存在通知设置==>新增
            notificationParam.setUserId(user.getId());//user_id在tb_notification表中不是主键，需要设置
            notificationService.save(notificationParam);
        }else{
            //3.2存在通知设置==>更新
            notification.setGonggaoNotification(notificationParam.getGonggaoNotification());
            notification.setLikeNotification(notificationParam.getLikeNotification());
            notification.setPinglunNotification(notificationParam.getPinglunNotification());
            notificationService.update(notification);
        }




    }

    //分页查询黑名单列表
    public PageBeanVo findBlackList(Integer pageNum, Integer pageSize) {

        //1.获取当前用户id
        Long userId = UserHolder.get().getId();

        //2.调用service查询
        return  blackListService.findBlackList(userId,pageNum,pageSize);

    }

    //移除黑名单
    public void deleteBlackList(Long blackUserId) {

        //1.获取当前用户id
        Long userId = UserHolder.get().getId();

        //2.调用service，根据当前登录用户的id和选中要删除的黑名单id 去删除黑名单记录
        blackListService.delete(userId,blackUserId);

    }
}
